package me.leon.ext.android

import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import kotlin.math.roundToInt

/**
 * @author : Leon Shih
 * @time : 2019/12/16 0016 17:50
 * @e-mail : deadogone@gmail.com :
 * @desc : View extension
 */
fun View.dp2px(dp: Int) = (resources.displayMetrics.density * dp).roundToInt()

fun View.colorRes(@ColorRes colorRes: Int): Int = context.colorRes(colorRes)

fun View.drawableRes(@DrawableRes drawableRes: Int): Drawable? = context.drawableRes(drawableRes)

fun View.visible(isVisible: Boolean) {
    this.visibility = if (isVisible) View.VISIBLE else View.INVISIBLE
}

fun View.visibleOrGone(isVisible: Boolean) {
    this.visibility = if (isVisible) View.VISIBLE else View.GONE
}
